// pages/promise/promise.js
let app = getApp();
const culApi = require("../apis/cultivation");
Page({
  data: {
    enable: false,
    loadingProps: {
      size: '40rpx',
    },
    backTopVisible: false,
    title: "养殖记录",
    state: ["","CULTIVATING","DISPATCHING","HISTORY"],
    value: "",
    index: '1',
    ids: [],
    trans:[],
    isnull: false,
    issearch: false,
    total: 0,
    ischange: false,
    farmVisible: false,
    farms: [],
    farmValue: "",
    farmText: "",
    amount: 100,
    trans_amount:100,
    count:0,
    minDate: new Date(2023, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    dateSelectNote: "",
    product: {
      value: "all",
      options: [
        {
          value: "all",
          label: "全部",
        },
        {
          value: "creation",
          label: "未入栏",
        },
        {
          value: "lairaging",
          label: "未出栏",
        },
      ],
    },
    sorterFieldvisible: true,
    sorterField: {
      value: "createTime",
      options: [
        {
          value: "createTime",
          label: "创建时间",
        },
        {
          value: "entryTime",
          label: "入栏时间",
        },
        {
          value: "leaveTime",
          label: "出栏时间",
        },
      ],
    },
    sorter: {
      value: "default",
      options: [
        {
          value: "default",
          label: "默认排序",
        },
        {
          value: "asc",
          label: "从以前到最近",
        },
      ],
    },
    visible: false,
    isShow: false,
  },
  onProductChange(e) {
    this.setData({
      "sorterFieldvisible": e.detail.value === "all",
      "product.value": e.detail.value,
    });
  },
  onSorterFieldChange(e) {
    this.setData({
      "sorterField.value": e.detail.value,
    });
  },
  onSorterChange(e) {
    this.setData({
      "sorter.value": e.detail.value,
    });
  },
  handleCalendar() {
    this.setData({ visible: true });
  },
  handleConfirm(e) {
    const { value } = e.detail;
    const format = (val) => {
      const date = new Date(val);
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    };
    this.setData({
      dateSelectNote: format(value[0]) + "到" + format(value[1]),
    });
    this.loadData(format(value[0]),format(value[1]));
  },
  onIDPicker() {
    this.setData({ farmVisible: true });
  },
  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;
    console.log("picker change:", e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: e.detail.label,
    });
    this.searchTrans();
  },
  onPickerCancel(e) {
    const { key } = e.currentTarget.dataset;
    console.log(e, "取消");
    console.log("picker1 cancel:");
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  change(e) {
    this.setData({
      ischange: e.detail,
    });
  },
  add() {
    app.globalData.operation = 1;
    app.globalData.promise_data = {
      id: null,
      manger: "",
      farmId: "",
      entryTime: "",
      leaveTime: "",
      picture: [],
    };
    wx.navigateTo({
        url: "./add/add",
        events: null,
        success: (result) => { 
        },
        fail: (res) => {
          console.log("wrong",res);
         },
        complete: (res) => { },
      });
  },
  load(){
    // TODO 暂时用分页接口演示
    this.setData({
      isShow: false,
    })
    culApi.getCultivation(1,this.data.amount,this.data.state[this.data.index-1],null).then(data=>{
      console.log("1.datas",data);
      let isnull = false;
      if(!data||data.length<=0){
        isnull = true;
      }
       this.setData({
         ids: data,
         issearch: false,
         isnull: isnull,
         value: "",
         isShow:true,
       });
    });
  },
  changeindex(e){
    console.log(e);
    this.setData({
      index: e.detail.value
    })
    this.load()
  },
  onLoad() {
    this.load();
  },
  onShow() {
    this.load();
  }, 
  onRefresh() {
    this.setData({ enable: true });
    this.load();
    setTimeout(() => {
      this.setData({ enable: false });
    }, 500);
  },
  onScroll(e) {
    const { scrollTop } = e.detail;
    this.setData({
      backTopVisible: scrollTop > 100,
    });
  }
});
